package cn.ulyer.common.oauth;


import cn.ulyer.common.utils.Result;
import cn.ulyer.common.utils.WebUtils;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.access.AccessDeniedHandler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 自定义访问拒绝
 * @author ulyer
 */
@Slf4j
public class JsonOauthResourceFailHandler implements AccessDeniedHandler , AuthenticationEntryPoint {



    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response,
                         AuthenticationException exception)  {
        WebUtils.writeJson(response, JSONObject.toJSONString(Result.fail(exception.getMessage())));
    }

    @Override
    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse response, AccessDeniedException e)  {
        WebUtils.writeJson(response, JSONObject.toJSONString(Result.fail(e.getMessage())));

    }


}
